﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json.Linq;

namespace DataIssue.Service
{
    /// <summary>
    /// 龙虎榜
    /// </summary>
    public class StockSummaryService
    {
        /// <summary>
        /// 上榜股票统计
        /// </summary>
        /// <returns></returns>
        public List<Entity.StockSummary> GetStocks()
        {
            List<Entity.StockSummary> datas = new List<Entity.StockSummary>();
            F10ParamEntity param = new F10ParamEntity("Base", "V_DR_STK_PUBTRDSTK", 2);
            param.Filters = new List<string>();
            param.Filters.Add("STCPD-int=1");
            param.Limit = 8;
            param.OrderBy = "TRTO DESC";

            JToken clientData;
            int totalCount = 0;
            F10Query.TryParse(param, false, out clientData, out totalCount);
            if (clientData != null && totalCount > 0)
            {
                for (int i = 0; i < clientData.Count(); i++)
                {
                    double temp = 0f;
                    int intTemp = 0;
                    Entity.StockSummary data = new Entity.StockSummary();
                    data.Account = clientData[i].J_Double("TRTO", temp);
                    data.Code = clientData[i].J_String("STKCODE");
                    data.Count = clientData[i].J_Integer("TOTCT", intTemp);
                    data.Name = clientData[i].J_String("STKABB");
                    datas.Add(data);
                }
            }
            return datas;
        }

        /// <summary>
        /// 上榜营业厅统计
        /// </summary>
        /// <returns></returns>
        public List<Entity.StockSummary> GetYinYeTing()
        {
            List<Entity.StockSummary> datas = new List<Entity.StockSummary>();
            F10ParamEntity param = new F10ParamEntity("Base", "V_DR_STK_PUBTRDBRH", 2);
            param.Filters = new List<string>();
            param.Filters.Add("STCPD-int=1");
            param.Limit = 8;
            param.OrderBy = "TRTO DESC";

            JToken clientData;
            int totalCount = 0;
            F10Query.TryParse(param, false, out clientData, out totalCount);
            if (clientData != null && totalCount > 0)
            {
                for (int i = 0; i < clientData.Count(); i++)
                {
                    double temp = 0f;
                    int intTemp = 0;
                    Entity.StockSummary data = new Entity.StockSummary();
                    data.Account = clientData[i].J_Double("TRTO", temp);
                    data.Count = clientData[i].J_Integer("TOTCT", intTemp);
                    data.Name = clientData[i].J_String("BRHNAME");
                    data.Code = clientData[i].J_Integer("BRHCODE", 0).ToString();
                    datas.Add(data);
                }
            }
            return datas;
        }

        /// <summary>
        /// 机构席位追踪
        /// </summary>
        /// <returns></returns>
        public List<Entity.StockSummary> GetJiGou()
        {
            List<Entity.StockSummary> datas = new List<Entity.StockSummary>();
            F10ParamEntity param = new F10ParamEntity("Base", "V_DR_STK_PUBTRDORG", 2);
            param.Filters = new List<string>();
            param.Filters.Add("STCPD-int=1");
            param.Limit = 8;
            param.OrderBy = "TRTO DESC";

            JToken clientData;
            int totalCount = 0;
            F10Query.TryParse(param, false, out clientData, out totalCount);
            if (clientData != null && totalCount > 0)
            {
                for (int i = 0; i < clientData.Count(); i++)
                {
                    double temp = 0f;
                    int intTemp = 0;
                    Entity.StockSummary data = new Entity.StockSummary();
                    data.Account = clientData[i].J_Double("TRTO", temp);
                    data.Code = clientData[i].J_String("STKCODE");
                    data.Count = clientData[i].J_Integer("TOTCT", intTemp);
                    data.Name = clientData[i].J_String("STKABB");
                    datas.Add(data);
                }
            }
            return datas;
        }
    }
}